/*
 * @Author: jqgao2
 * @Date: 2022-03-23 21:07:43
 * @LastEditTime: 2022-03-23 22:36:17
 * @Description:
 * @FilePath: \ilovecoffee\src\coffees\coffees.service.ts
 * @LastEditors: jqgao2
 */
import {
  HttpException,
  HttpStatus,
  Injectable,
  NotFoundException,
} from '@nestjs/common';
import { Coffee } from './entities/coffee.entity';

@Injectable()
export class CoffeesService {
  private coffees: Coffee[] = [
    {
      id: 1,
      name: 'abner',
      brand: 'china',
      flavors: ['33', '44'],
    },
  ];

  findAll() {
    return this.coffees;
  }

  findOne(id: string) {
    const coffee = this.coffees.find((item) => item.id === +id);
    if (!coffee) {
      throw new NotFoundException(`coffee ${id} not found`);
    }

    return coffee;
  }

  create(createCoffeeDto: any) {
    this.coffees.push(createCoffeeDto);
    return createCoffeeDto;
  }

  update(id: string, updateCoffeeDto: any) {
    const exitingCoffee = this.findOne(id);
    if (exitingCoffee) {
      //update
    }
  }
  remove(id: string) {
    const coffeeIndex = this.coffees.findIndex((item) => item.id === +id);
    if (coffeeIndex >= 0) {
      this.coffees.splice(coffeeIndex, 1);
    }
  }
}
